from pprint import pprint as pp

from requests import get
from pymysql import connect
from pymysql.cursors import DictCursor

url = "http://albeibei.cn:8886"
# cookies = {
#     "JSESSION": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6ImIyYi0yOTk4MjEzNzQ3MDk2OGEiLCJleHAiOjE1OD"
#                 "YzOTUxMzR9.a-p3FXWHsH--IZlkVsIUAWT5p0F5pxkq1ZWK-2oTAQk"
# }


# cookies = {
#     "JSESSION": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6ImIyYi0yOTE5OTQ2Mzk2OGY2ZWMiLCJleHAiOjE1O"
#                 "DY0MTQxMzl9.k4GsLsXTfSWEbEiJ7P8IVttV6iX69bXhdAgx8YZAUbQ"
# }

cookies = {
    "JSESSION": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6ImIyYi0yODQ1Njg0NTU3NDZjZmUiLCJleHAiOjE1O"
                "DY0ODc0ODJ9.cBFvlhdktJWgtp9w_lx-jYUmhmC-6T6wIRMUGDlizLQ"
}


class Mysql:
    def __init__(self):
        self.connection = connect(
            host="120.77.183.17",
            user="kxx",
            password="$5#g#hWS@Es7mexj",
            charset="utf8",
            cursorclass=DictCursor)

    def select(self, sql_):
        with self.connection.cursor() as cursor:
            cursor.execute(sql_)
            result = cursor.fetchall()

        return result

    def insert(self, sql_):
        with self.connection.cursor() as cursor:
            try:
                cursor.execute(sql_)
                self.connection.commit()
            except Exception as e:
                print(e)
                self.connection.rollback()

    def close(self):
        self.connection.close()


def get_token():
    resp = get(url + "/oo/v1/get/code", params={"code": "cbae399c-71c8-45c6-a2d0-fadb602febfd"}).json()
    print(resp)


def select_offer():
    resp = get(url + "/oo/v1/get/offer/1/20", cookies=cookies)
    return resp.json()


def add_offer():
    resp = select_offer()

    for offer in resp['result']['pageResult']['resultList']:
        resp = get(url + "/oo/v1/pull/offer", json=offer, cookies=cookies)
        if resp.status_code != 200:
            pp(offer)
            break
        print(resp.json())


def push_offer():
    sql = Mysql()
    result = sql.select("SELECT product_id FROM alibb.alibb_container_product WHERE is_use='0'")
    sql.close()
    for data in result:
        resp = get(url + "/oo/v1/push/offer", json={"product_id": data['product_id']}, cookies=cookies)
        if resp.status_code != 200:
            print(data['product_id'])
            print(resp.json())
            break

        print(resp.json())


def delete_offer():
    resp = get(url + "/oo/v1/delete/offer", json={"webSite": '1688', "product_id": "615426245338"}, cookies=cookies)
    print(resp.json())


def test_celery():
    resp = get(url + "/oo/v1/celery/test")
    print(resp.status_code)
    print(resp.text)


if __name__ == '__main__':
    # ==============增===================
    # get_token()  # 手动授权
    # add_offer()  # 保存商品
    # push_offer()  # 恢复商品
    test_celery()

    # ==============删===================
    # delete_offer()  # 删除商品

    # ==============改===================

    # ==============查===================
    # select_offer()
